package com.fh.modules.ems.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fh.common.api.vo.Result;
import com.fh.common.aspect.annotation.AutoLog;
import com.fh.common.constant.CommonConstant;
import com.fh.common.query.QueryWrapperUtil;
import com.fh.modules.ems.entity.EmsCouponTemplate;
import com.fh.modules.ems.service.IEmsCouponTemplateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;

/**
 * @Title: Controller
 * @Description: 优惠券模板
 * @author： code generator
 * @date：   2019-09-26
 * @version： V1.0
 */
@RestController
@Slf4j
@Api("优惠券模板")
public class EmsCouponTemplateController {
	@Autowired
	private IEmsCouponTemplateService emsCouponTemplateService;
	
	/**
	  * 分页列表查询
	 * @param emsCouponTemplate
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@GetMapping(value = "/ems/emsCouponTemplate/list")
	@ApiOperation("分页列表查询")
	@AutoLog(value="优惠券模板分页查询",logType=CommonConstant.LOG_TYPE_QUERY)
	public Result queryPageList(EmsCouponTemplate emsCouponTemplate,
									  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
									  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
									  HttpServletRequest req) {
		QueryWrapper<EmsCouponTemplate> queryWrapper = new QueryWrapper<EmsCouponTemplate>();
		emsCouponTemplate.setDelFlag(CommonConstant.DEL_FLAG_0);
		QueryWrapperUtil.initQueryWrapper(queryWrapper, emsCouponTemplate);
		Page<EmsCouponTemplate> page = new Page<EmsCouponTemplate>(pageNo, pageSize);
		IPage<EmsCouponTemplate> pageList = emsCouponTemplateService.page(page, queryWrapper);
		return Result.ok(pageList);
	}
	
	/**
	  *   添加优惠券模板
	 * @param emsCouponTemplate
	 * @return
	 */
	@PostMapping(value = "/ems/emsCouponTemplate/add")
	@ApiOperation("添加")
	@AutoLog("添加优惠券模板")
	public Result add(@RequestBody EmsCouponTemplate emsCouponTemplate) {
        emsCouponTemplate.setPlatform(0);
		emsCouponTemplate.setVersion(1);
        emsCouponTemplate.setMinPoint(new BigDecimal("0"));
        emsCouponTemplate.setUseType(0);
        emsCouponTemplate.setPublishStatus(0);
        emsCouponTemplate.setOverdueCount(0);
        emsCouponTemplate.setReceiveCount(0);
        emsCouponTemplate.setUseCount(0);
		emsCouponTemplateService.save(emsCouponTemplate);
		return Result.ok("添加成功");
	}
	
	/**
	  *  修改优惠券模板,
     *  1、已发布或者已下线的优惠券模板不能修改
     *  2、对优惠券的操作是通过“优惠码”，比如全场送和购物送通过优惠码设置
     *  3、优惠券上线发布后可以手动下线，优惠券状态转换只有如下流程:未发布->已发布->已下线
     *  4、优惠券模板不能删除
	 * @param emsCouponTemplate
	 * @return
	 */
	@PutMapping(value = "/ems/emsCouponTemplate/edit")
	@ApiOperation("修改")
	@AutoLog("修改优惠券模板")
	public Result edit(@RequestBody EmsCouponTemplate emsCouponTemplate) {
		boolean ok = emsCouponTemplateService.updateById(emsCouponTemplate);
		return ok?Result.ok("修改成功"):Result.error("修改失败");
	}

	/**
	 *  删除
	 * @param id
	 * @return
	 */
	@DeleteMapping(value = "/ems/emsCouponTemplate/delete")
	@ApiOperation("删除")
	@AutoLog("删除优惠券模板")
	public Result delete(@RequestParam(name="id",required=true) String id) {
		emsCouponTemplateService.deleteEntityByDelFlag(id);
		return Result.ok("删除成功");
	}

	/**
	  *  批量删除
	 * @param ids
	 * @return
	 */
	@DeleteMapping(value = "/ems/emsCouponTemplate/deleteBatch")
	@ApiOperation("批量删除")
	@AutoLog("批量删除优惠券模板")
	public Result deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		emsCouponTemplateService.deleteBatchEntityByDelFlag(ids);
		return Result.ok("批量删除成功");
	}
	
	/**
	  * 通过id查询
	 * @param id
	 * @return
	 */
	@GetMapping(value = "/ems/emsCouponTemplate/queryById")
	@ApiOperation("通过id查询")
	@AutoLog(value="优惠券模板通过id查询",logType=CommonConstant.LOG_TYPE_QUERY)
	public Result queryById(@RequestParam(name="id",required=true) String id) {
		EmsCouponTemplate emsCouponTemplate = emsCouponTemplateService.getById(id);
		return Result.ok(emsCouponTemplate);
	}

}
